module.exports = function (app) {

    var demoData = require("../JSON/demo.json");
    var starIndex = 10;
    //查询列表 GET方法
    app.get("/api/messages", function (req, res, next) {
        console.log('get record list');
        res.json(demoData['messages']);
    });

    //新增列表记录 POST方法
    app.post("/api/messages", function (req, res, next){
        console.log('add a new record');
        console.log(req.body);
//        req.body.messageId = starIndex++;
//        res.json(req.body);
        res.json({messageId:starIndex++, type: 'ADD'});
    });

    //按唯一id查询列表中单条记录 GET方法
    app.get("/api/messages/:id", function (req, res, next) {
        console.log("search a record by id");
        res.json( demoData[req.params['id']]? demoData[req.params['id']]: {messageId: req.params['id'], content:'server text',type: 'S'} );
    });

    //按唯一id更新单条记录全部字段 PUT方法
    app.put("/api/messages/:id", function (req, res, next) {
        console.log("update all fields of a record by id");
        console.log(req.body);
        res.json( {messageId: req.params['id'], type: 'PUT UPDATE'} );
    });

    //按唯一id更新单条记录部分字段 PATCH方法
    app.patch("/api/messages/:id", function (req, res, next) {
        console.log("update partial fields of a record by id");
        console.log(req.body)
        //res.json( demoData[req.params['id']]? demoData[req.params['id']]: {content:'server text',type: 'S'} );
        res.json( {messageId: req.params['id'], type: 'PATCH UPDATE'} );
    });

    //按唯一id删除单条记录
    app.delete("/api/messages/:id", function (req, res, next) {
        console.log("delete a record by id");
        console.log(req.body);
        res.json({message:'Delete from server success'});
    });

}